from flask import Blueprint,render_template, request,session,redirect
from flask.helpers import url_for
from config import conn,cur,mutex ,r as redis
import pymssql
Registration = Blueprint('Registration',__name__)
 
@Registration.route('/',methods=['GET','POST'])
def index():
    return render_template('/registration/index.html',)

@Registration.route('/success',methods=['GET','POST'])
def success():
   id = request.args.get('id')
   return render_template('/registration/registration_success.html',id=id)

@Registration.route('/insertUser',methods=['GET','POST'])
def insertUser():
   if request.method == "POST":
      username = request.form.get("username")
      password = request.form.get("password")
      gender = request.form.get("gender")
      birthday = request.form.get("birthday")
      weight = request.form.get("weight")
      height = request.form.get("height")
      doc_id = request.form.get("doc_id")
      doc_visit_date = request.form.get("doc_visit_date")
      annual_checkup = request.form.get("annual_checkup")
   
   print('birthday:',birthday)
   print('doc_visit_date:',doc_visit_date)
   print('annual_checkup:', annual_checkup)
   username = "'"+ username +"'"
   password = "'"+ password +"'"
   gender = "'"+ gender +"'"
   birthday = "NULL" if birthday == '' else "'"+ birthday +"'"
   weight = "NULL" if weight == '' else weight
   height = "NULL" if height == '' else height
   doc_id = "NULL" if doc_id == '' else "'"+ doc_id +"'"
   doc_visit_date = "NULL" if doc_visit_date == '' else "'"+ doc_visit_date +"'"
   annual_checkup = "NULL" if annual_checkup == '' else "'"+ annual_checkup +"'"
   try:
      # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
      sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_id) VALUES ({},{},{},{},{},{},{});".format(username,password,gender,birthday,weight,height,doc_id)
      print(sqlstr)
      mutex.acquire()
      cur.execute(sqlstr)
      mutex.release()
      conn.commit()

      id_sql = "select * from personal_info where name={} and password ={} order by id DESC;".format(username, password)
      print(id_sql)
      cur.execute(id_sql)
      row = cur.fetchone()
      # print(row)
      ID = row[0]
      # print(ID)
      if doc_visit_date != "NULL":
         doc_vis_sql = "insert into  doctor_visits  (person_id,date) VALUES ({},{});".format(ID,doc_visit_date)
         print(doc_vis_sql)
         mutex.acquire()
         cur.execute(doc_vis_sql)
         mutex.release()
         conn.commit()


      if annual_checkup != "NULL":
         ann_chk_sql = "insert into  annual_checkups  (person_id,date) VALUES ({},{});".format(ID,annual_checkup)
         print(ann_chk_sql)
         mutex.acquire()
         cur.execute(ann_chk_sql)
         mutex.release()
         conn.commit()

      return {'message':'success','id':ID}
   except Exception as re:
      print(re)
      return {'message':'error_registration'}

@Registration.route('/insertdoctor',methods=['GET','POST'])
def insertdoctor():
   if request.method == "POST":
      username = request.form.get("username")
      password = request.form.get("password")

   username = "'"+ username +"'"
   password = "'"+ password +"'"
   try:
      # sqlstr="insert into  personal_info  (name,password,gender,age,weight,height,doctor_name,doc_visit_date,annual_checkup) VALUES ({},{},{},{},{},{},{},{},{})".format(username,password,gender,age,weight,height,doc_name,doc_visit_date,annual_checkup)
      sqlstr="insert into doctor_info (name,password) VALUES ({},{});".format(username,password)
      print(sqlstr)
      mutex.acquire()
      cur.execute(sqlstr)
      mutex.release()
      conn.commit()

      id_sql = "select * from doctor_info where name={} and password ={} order by id DESC;".format(username, password)
      print(id_sql)
      cur.execute(id_sql)
      row = cur.fetchone()
      print(row)
      ID = row[0]
      print(ID)

      return {'message':'success','id':ID}
   except Exception as re:
      print(re)
      return {'message':'error_registration'}

@Registration.route('/doctor',methods=["GET", "POST"])
def doctorregist():
    return render_template('/doctor/regist.html')
   